"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 日志目录
Case Name   : 记录系统日志过程中日志目录变为不存在
Create At   : 2020/07/01
Owner       : @wan005
Description :
    1.执行TPCC
    2.执行TPCC过程中删除日志目录
    3.检查数据库状态
    4.检查tpcc执行结果
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
    Modified by @zou_jialiang050 2022/04/26 : 确认备份文件夹是否存在，不存在则新建备份文件夹
"""
import os
import sys
import unittest
import _thread
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread


class SysLog(unittest.TestCase):
    sh = CommonSH('PrimaryDbUser')
    log = Logger()
    dbUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRoot = Node(node='PrimaryRoot')
    com = Common('PrimaryRoot')
    Constant = Constant()

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('--------------备份文件夹--------------------------- ')
        is_dir_exists_cmd = f'''if [ ! -d "{macro.DB_BACKUP_PATH}" ]
                                        then
                                            mkdir {macro.DB_BACKUP_PATH}
                                        fi'''
        result = self.dbUserNode.sh(is_dir_exists_cmd).result()
        self.log.info(result)

    def test_del_log(self):
        self.log.info('------执行TPCC------ ')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.dbPrimaryRoot, macro.TPCC_PATH))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        self.log.info('------执行TPCC过程中删除日志目录------')
        cmd = f'cp -R {macro.GAUSSDB_LOG_PATH} {macro.DB_BACKUP_PATH}; ' \
            f'rm -rf {macro.GAUSSDB_LOG_PATH}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.assertEqual(msg, '')
        msg = self.dbUserNode.sh(f'ls -al {macro.GAUSSDB_LOG_PATH}').result()
        self.log.info(msg)
        msg = self.dbUserNode.sh(f'ls -al {macro.DB_BACKUP_PATH}').result()
        self.log.info(msg)

        time.sleep(5)

        self.log.info('------检查数据库状态------')
        db_status = self.sh.getDbClusterStatus('status')
        self.log.info(db_status)
        self.assertTrue(db_status)

        self.log.info('------检查tpcc执行结果------')
        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

    def tearDown(self):
        self.log.info('-----恢复配置文件-----')
        cmd = f'rm -rf {macro.GAUSSDB_LOG_PATH};' \
            f'mv {os.path.join(macro.DB_BACKUP_PATH, os.path.basename(macro.GAUSSDB_LOG_PATH))} {os.path.dirname(macro.GAUSSDB_LOG_PATH)}'
        self.log.info(cmd)
        msg = self.dbUserNode.sh(cmd).result()
        self.log.info(msg)
        self.assertEqual(msg, '')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
